<!--
 * @Descripttion: 发放记录
 * @Author: xiao li
 * @Date: 2020-07-06 12:17:07
 * @LastEditors: xiao li
 * @LastEditTime: 2021-04-29 11:35:49
-->
<template>
    <div class="lb-sys-sproconfig">
        <top-nav :title="navTitle" :isBack="true"></top-nav>
        <div class="page-main">
          <el-row class="page-search-form">
            <el-form
            :inline="true"
            :model="searchForm"
            ref="searchForm"
            >

              <el-form-item label="员工姓名/昵称" prop="name">
                <el-input v-model="searchForm.name" placeholder="请输入员工姓名/昵称"></el-input>
              </el-form-item>
              <el-form-item>
                <lb-button size="medium" type="primary" icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size="medium" icon="el-icon-refresh-left" style="margin-right:5px" @click="resetForm('searchForm')">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </el-row>

            <el-table
            v-loading="loading"
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            style="width: 100%">
            <el-table-column
              prop="staff_id"
              label="员工ID">
            </el-table-column>
            <el-table-column
              prop="staff_name"
              label="员工姓名">
            </el-table-column>
            <el-table-column
              prop="nickName"
              label="员工昵称">
            </el-table-column>
            <el-table-column
              prop="share_num"
              label="分享新客福包次数">
            </el-table-column>
            <el-table-column
              prop="pv"
              label="浏览人数">
            </el-table-column>
            <el-table-column
              prop="status_text"
              label="剩余金额｜总金额">
              <template slot-scope="scope">
                <div>¥{{scope.row.y_price + ' | ¥' + scope.row.total_price}}</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="create_time"
              label="剩余｜总量">
              <template slot-scope="scope">
                <div>{{scope.row.y_num + '个 | ' + scope.row.total_num}}个</div>
              </template>
            </el-table-column>
            <el-table-column
              label="操作">
                <template slot-scope="scope">
                  <div class="table-operate">
                    <!-- status_text 1未开始，2进行中，3已结束-->
                    <lb-button size='mini' plain type='danger' @click="toEnd(scope.row.id)" v-if="scope.row.status === 0">停止发放</lb-button>
                    <lb-button size='mini' plain type='primary' @click="$router.push('/app/cash/detail?id=' + scope.row.active_id + '&uid=' + scope.row.staff_id)">{{$t('action.view')}}</lb-button>
                  </div>
                </template>
            </el-table-column>
          </el-table>

        <lb-page
          :batch='false'
          :page='searchForm.page'
          :pageSize='searchForm.limit'
          :total='total'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
          >
        </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      loading: false,
      total: 0,
      searchForm: {
        name: '',
        active_id: 0,
        page: 1,
        limit: 10
      },
      tableData: []
    }
  },
  activated () {
    let { id } = this.$route.query
    if (id) {
      this.searchForm.active_id = id
      this.getTableDataList()
    }
  },
  methods: {
    resetForm (name) {
      this.$refs[name].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      let {searchForm} = this
      let {code, data} = await this.$api.cashStaffRecordList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
      this.navTitle = data.active_name ? data.active_name + '的发放记录' : '发放记录'
    },
    async toEnd (id) {
      let confirmText = this.$t('tips.confirmStopProvide')
      this.$confirm(confirmText, this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.toConfirmEnd(id)
      }).catch(() => {

      })
    },
    async toConfirmEnd (id) {
      let {code} = await this.$api.cashStopRecord({id})
      if (code !== 200) return
      this.$message.success(this.$t('tips.successOper'))
      this.getTableDataList()
    }
  }
}
</script>

<style lang="scss" scoped>
.el-input,
.el-select{
  width: 200px;
}
</style>
